package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.shop.bo.GoodsDataHistoryBO;
import com.junzhiit.nengji.shop.dao.GoodsDataHistoryDAO;
import com.junzhiit.nengji.shop.model.GoodsDataHistory;
import com.junzhiit.nengji.shop.model.enums.Is;

@Repository
public class GoodsDataHistoryDAOImpl extends AbstractBaseDAO<GoodsDataHistory> implements GoodsDataHistoryDAO{


	@Override
	public List<GoodsDataHistoryBO> findAll(String ordersDataHistoryId) {
		StringBuffer sql = new StringBuffer();
		
		sql.append("select g.*,t.full_name 'type_name'");
		sql.append(" from goods_data_history g inner join type t on g.type_id = t.id");
		sql.append(" where g.is_delete = ? and g.orders_data_history_id = ?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		param.setString(ordersDataHistoryId);
		
		return queryForList(sql.toString(), GoodsDataHistoryBO.class, param);
	}

	@Override
	public int getTotal(GoodsDataHistoryBO GoodsDataHistoryBO) {
		StringBuffer sql = new StringBuffer();
		
		sql.append("select count(g.id)");
		sql.append(" from goods_data_history g inner join type t on g.type_id = t.id");
		sql.append(" where g.is_delete = ?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		if(StringUtils.isNotBlank(GoodsDataHistoryBO.getStartTime())){
			sql.append(" and o.created_time >= ?");
			param.setDate(DateUtil.getDateStart(DateUtil.parseDate(GoodsDataHistoryBO.getStartTime())));
		}
		
		if(StringUtils.isNotBlank(GoodsDataHistoryBO.getEndTime())){
			sql.append(" and o.created_time <= ?");
			param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(GoodsDataHistoryBO.getEndTime())));
		}
		
		return queryForInt(sql.toString(),param);
	}

	@Override
	public List<GoodsDataHistory> findList(String ordersDataHistoryId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from goods_data_history where orders_data_history_id = ?");
		StatementParameter params = new StatementParameter();
		params.setString(ordersDataHistoryId);
		return super.queryForList(sql.toString(), GoodsDataHistory.class,params);
	}

	@Override
	public List<GoodsDataHistoryBO> shopFindGoods(String ordersDataHistoryId) {
		StringBuffer sql = new StringBuffer();
		
		sql.append("select t.full_name 'type_name',g.goods_name,g.sku_content_name,");
		sql.append("g.price,g.number,g.coupons_money,g.is_open_rebate,g.profit,g.is_share,");
		sql.append("g.profit_money,g.shop_get_benefit_ration,g.shop_get_benefit_money,g.shop_money");
		sql.append(" from goods_data_history g inner join type t on g.type_id = t.id");
		sql.append(" where g.is_delete = ? and g.orders_data_history_id = ?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		param.setString(ordersDataHistoryId);
		
		return queryForList(sql.toString(), GoodsDataHistoryBO.class, param);
	}

}
